package org.flintparticles.twoD.stage3d.images
{
	import flash.display3D.Context3DBlendFactor;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Program3D;
	
	import org.flintparticles.common.stage3d.Vao;
	import org.flintparticles.common.stage3d.utils.Stage3DMath;

	public class Polygon2D extends GeomEntity2D
	{
		
		private var  alphaVector:Vector.<Number> = new <Number>[alpha, alpha, alpha, alpha];
		public function Polygon2D(width:Number=10, height:Number=10, color:uint=0xffffff)
		{

		   _vao=new Vao(4,true);
		   _vao.setPosition(0,0,0);
		   _vao.setPosition(1, width, 0.0);
		   _vao.setPosition(2, 0.0, height);
		   _vao.setPosition(3, width, height);            
		   _vao.setUniformColor(color);
		}

	public override function draw(stageMath:Stage3DMath):void{
			//_context.clear();
			alphaVector= new <Number>[alpha, alpha, alpha, alpha];
				
			
			
			if (_context == null) throw new Error("The context3D is not set!!!");
			
			
			

			stageMath.pushMatrix();
			stageMath.transformMatrix(this );
			///setting buffer for vertices///
	
			///setting transformation matrix uniform
		//	trace("MVP Matrix is:"+stageMath.mvpMatrix.position);
			//_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, stageMath.mvpMatrix, true);
			/////changed to :now passing model and projection matrices to GPU to offload CPU calcualtions,the MVP result matrix is
			////calcualted in the Vertex Shader///
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, stageMath.mModelViewMatrix, true); ///stageMath.mvpMatrix
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 4, stageMath.mProjectionMatrix, true); 
			///setting alpha uniform
		   /////upload alpha/////
			_context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 0, alphaVector, 1);
				
			
		
			//drawing///
			_context.drawTriangles(_indexBuffer, 0, 2);
			
			////unbound buffers////////
		//	_context.setVertexBufferAt(0, null);
		//	_context.setVertexBufferAt(1, null);
		
			stageMath.popMatrix();
		}
		public override function dispose():void{
			if(_vertexBuffer)_vertexBuffer.dispose();
			if(_indexBuffer)_indexBuffer.dispose();
			super.dispose();
			
		}

		public function setDefaultBlendFactors(premultipliedAlpha:Boolean):void
		{
			var destFactor:String = Context3DBlendFactor.ONE_MINUS_SOURCE_ALPHA;
			var sourceFactor:String = premultipliedAlpha ? Context3DBlendFactor.ONE :
				Context3DBlendFactor.SOURCE_ALPHA;
			_context.setBlendFactors(sourceFactor, destFactor);
		}
		

		/** Returns the color of the quad, or of vertex 0 if vertices have different colors. */
		public function get color():uint 
		{ 
			return _vao.getColor(0); 
		}
		/** Sets the colors of all vertices to a certain value. */
		public function set color(value:uint):void 
		{
			if(value ==color){
				return;
			}
			_vao.setUniformColor(value);
			
			if (_vertexBuffer) updateVertexBuffer();
			
		}
		
		public function clone():Polygon2D{
			var clone:Polygon2D=new Polygon2D();
			clone.color=color;
			clone.vertexData=vertexData;
			clone.shaderProgram=this.shaderProgram;
			clone.context=this.context;
			return clone;
			
		}
		////////////////////buffers init///////////////////////////////
		
		
		
		

		public function get shaderProgram():Program3D
		{
			return null;
		}

		public function set shaderProgram(value:Program3D):void
		{
			//_shaderProgram = value;
		}

		

		
		/////////////////////////////////////////////////////////////////
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}